home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Format CD 42
/
Amiga Format AFCD42 (Issue 126, Aug 1999).iso
/
-serious-
/
programming
/
other
/
jikes
/
src
/
unzip.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
1999-05-14
|
27KB
|
915 lines
// $Id: unzip.cpp,v 1.2 1999/01/13 21:48:48 shields Exp $
//
// NOTE: Jikes incorporates compression code from the Info-ZIP
// group. There are no extra charges or costs due to the use of
// this code, and the original compression sources are freely
// available from http://www.cdrom/com/pub/infozip/ or
// ftp://ftp.cdrom.com/pub/infozip/ on the Internet.
// The sole use by Jikes of this compression code is contained in the
// files unzip.h and unzip.cpp, which are based on Info-ZIP's inflate.c and
// associated header files.
//
//
// You can do whatever you like with this source file, though I would
// prefer that if you modify it and redistribute it that you include
// comments to that effect with your name and the date. Thank you.
// The abbreviated History list below includes the work of the
// following:
// M. Adler, G. Roelofs, J-l. Failly, J. Bush, C. Ghisler, A. Verheijen,
// P. Kienitz, C. Spieler, S. Maxwell, J. Altman
// Only the first and last entries from the original inflate.c are
// reproduced here.
//
//
// History:
// vers date who what
// ---- --------- -------------- ------------------------------------
// a ~~ Feb 92 M. Adler used full (large, one-step) lookup table
// ...
// c16 20 Apr 97 J. Altman added memzero(v[]) in huft_build()
//
#include "config.h"
#include "unzip.h"
unsigned long Unzip::global_bb; /* bit buffer */
unsigned Unzip::global_bk; /* bits in bit buffer */
unsigned Unzip::global_wp; /* current position in slide */
unsigned Unzip::global_hufts; /* huff memory usage */
unsigned char Unzip::slide_buffer[32768];
struct huft *Unzip::global_fixed_tl; /* inflate static */
struct huft *Unzip::global_fixed_td; /* inflate static */
int Unzip::global_fixed_bl,
Unzip::global_fixed_bd;
#if defined(UNIX_FILE_SYSTEM) || defined(AMIGAOS_FILE_SYSTEM)
FILE *Unzip::global_file; /* file pointer for zip file */
#elif defined(WIN32_FILE_SYSTEM)
char *Unzip::global_file; /* file pointer for zip file */
#endif
char *Unzip::global_bufferp; /* current position in output buffer */
/* Tables for deflate from PKZIP's appnote.txt. */
unsigned Unzip::border[] = { /* Order of the bit length code lengths */
16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
unsigned short Unzip::cplens[] = { /* Copy lengths for literal codes 257..285 */
3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
/* note: see note #13 above about the 258 in this list. */
unsigned short Unzip::cplext[] = { /* Extra bits for literal codes 257..285 */
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99}; /* 99==invalid */
unsigned short Unzip::cpdist[] = { /* Copy offsets for distance codes 0..29 */
1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
8193, 12289, 16385, 24577};
unsigned short Unzip::cpdext[] = { /* Extra bits for distance codes */
0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
12, 12, 13, 13};
/* moved to consts.h (included in unzip.c), resp. funzip.c */
/* And'ing with mask_bits[n] masks the lower n bits */
unsigned short Unzip::mask_bits[] = {
0x0000,
0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
};
int Unzip::lbits = 9; /* bits in base literal/length lookup table */
int Unzip::dbits = 6; /* bits in base distance lookup table */
struct huft *fixed_tl = (struct huft *) 0;
int Unzip::huft_build(unsigned *b,unsigned n, unsigned s, unsigned short *d, unsigned short *e, struct huft **t, int *m)
/*unsigned *b code lengths in bits (all assumed <= BMAX) */
/*unsigned n number of codes (assumed <= N_MAX) */
/*unsigned s number of simple-valued codes (0..s-1) */
/* unsigned short *d list of base values for non-simple codes */
/*ush *e list of extra bits for non-simple codes */
/*struct huft **t result: starting table */
/*int *m maximum lookup bits, returns actual */
/* Given a list of code lengths and a maximum table size, make a set of
tables to decode that set of codes. Return zero on success, one if
the given code set is incomplete (the tables are still built in this
case), two if the input is invalid (all zero length codes or an
oversubscribed set of lengths), and three if not enough memory.
The code with value 256 is special, and the tables are constructed
so that no bits beyond that code are fetched when that code is
decoded. */
{
unsigned a; /* counter for codes of length k */
unsigned c[BMAX+1]; /* bit length count table */
unsigned el; /* length of EOB code (value 256) */
unsigned f; /* i repeats in table every f entries */
int g; /* maximum code length */
int h; /* table level */
register unsigned i; /* counter, current code */
register unsigned j; /* counter */
register int k; /* number of bits in current code */
int lx[BMAX+1]; /* memory for l[-1..BMAX-1] */
int *l = lx+1; /* stack of bits per table */
register unsigned *p; /* pointer into c[], b[], or v[] */
register struct huft *q; /* points to current table */
struct huft r; /* table entry for structure assignment */
struct huft *u[BMAX]; /* table stack */
unsigned v[N_MAX]; /* values in order of bit length */
register int w; /* bits before this table == (l * h) */
unsigned x[BMAX+1]; /* bit offsets, then code stack */
unsigned *xp; /* pointer into x */
int y; /* number of dummy codes added */
unsigned z; /* number of entries in current table */
/* Generate counts for each bit length */
el = n > 256 ? b[256] : BMAX; /* set length of EOB code, if any */
memset((char *)c,0, sizeof(c));
p = b; i = n;
do {
c[*p]++; p++; /* assume all entries <= BMAX */
} while (--i);
if (c[0] == n) /* null input--all zero length codes */
{
*t = (struct huft *)0;
*m = 0;
return 0;
}
/* Find minimum and maximum length, bound *m by those */
for (j = 1; j <= BMAX; j++)
if (c[j])
break;
k = j; /* minimum code length */
if ((unsigned)*m < j)
*m = j;
for (i = BMAX; i; i--)
if (c[i])
break;
g = i; /* maximum code length */
if ((unsigned)*m > i)
*m = i;
/* Adjust last length count to fill out codes, if needed */
for (y = 1 << j; j < i; j++, y <<= 1)
if ((y -= c[j]) < 0)
return 2; /* bad input: more codes than bits */
if ((y -= c[i]) < 0)
return 2;
c[i] += y;
/* Generate starting offsets into the value table for each length */
x[1] = j = 0;
p = c + 1; xp = x + 2;
while (--i) { /* note that i == g from above */
*xp++ = (j += *p++);
}
/* Make a table of values in order of bit lengths */
memset((char *)v,0, sizeof(v));
p = b; i = 0;
do {
if ((j = *p++) != 0)
v[x[j]++] = i;
} while (++i < n);
n = x[g]; /* set n to length of v */
/* Generate the Huffman codes and for each, make the table entries */
x[0] = i = 0; /* first Huffman code is zero */
p = v; /* grab values in bit order */
h = -1; /* no tables yet--level -1 */
w = l[-1] = 0; /* no bits decoded yet */
u[0] = (struct huft *)0; /* just to keep compilers happy */
q = (struct huft *)0; /* ditto */
z = 0; /* ditto */
/* go through the bit len